import React, { Component } from 'react'
import SimEditor from 'simditor'
import 'simditor/styles/simditor.scss'

// 通用富文本编辑器，依赖jquery
class RichEditor extends Component {
    constructor(props){
        super(props);
    }
    componentDidMount(){
        this.loadEditor();
    }
    componentDidUpdate(prevProps) {
        if(prevProps.defaultDetail !== this.props.defaultDetail){
            this.editor.setValue(this.props.defaultDetail);
        }
    }
    loadEditor(){
        let oEditor = this.refs["texteditor"];
        this.editor = new SimEditor({
            textarea:$(oEditor),
            placeholder:this.props.placeholder || "请输入详情",
            upload:{
                url:'/manage/product/richtext_img_upload.do',
                defaultImage:"",
                fileKey:"upload_file"
            }
        })
        this.bindEditorEvent();
    }
    bindEditorEvent(){
        this.editor.on("valuechanged", e => {
            this.props.onValueChange(this.editor.getValue());
        })
    }
    render() {
        return (
            <div className="rich-editor">
                <textarea ref="texteditor"></textarea>
            </div>
        )
    }
}

export default RichEditor;